import pygame, sys, math, random
 
#PU: Explode, Speed, Freeze, Aura, Extra-Life, Instant-Death, Invisible-enemies.

class PowerUp():
    def __init__(self, pos):
        types = ["Speed", "Aura", "Slow", "Instant-Death", "Life", "God"]
        self.type = types[random.randint(0, 5)] #len(types)-1)]
        if self.type == "Speed":
            self.image = pygame.image.load("rsc/powerups/PowerUpSpeed.png")
        elif self.type == "Aura":
            self.image = pygame.image.load("rsc/powerups/PowerUpAura.png")
        elif self.type == "Slow":
            self.image = pygame.image.load("rsc/powerups/PowerUpSlow.png")
        elif self.type == "Instant-Death":
            self.image = pygame.image.load("rsc/powerups/PowerUpInstantDeath.png")
        elif self.type == "Life":
            self.image = pygame.image.load("rsc/powerups/PowerUpLife.png")
        elif self.type == "God":
            self.image = pygame.image.load("rsc/powerups/PowerUpGod.png")
        self.image = pygame.transform.scale(self.image, [50,50])
        self.rect = self.image.get_rect()
        self.place(pos)
        self.radius = self.rect.width/2
        self.touched = True
        self.lifetime = 540
        self.living = True
        
    def place(self, pos):
        self.rect.center = pos
    
    def collidePlayer(self, other):
        if self.rect.right > other.rect.left and self.rect.left < other.rect.right:
            if self.rect.bottom > other.rect.top and self.rect.top < other.rect.bottom:
                if self.radius + other.radius > self.distanceToPoint(other.rect.center):
                    self.living = False
                    if self.type == "Speed":
                        other.speedUp()
                    elif self.type == "Aura":
                        other.sizeUp()
                    elif self.type == "Slow":
                        other.slowDown()
                    elif self.type == "Instant-Death":
                        other.die()
                    elif self.type == "Life":
                        other.life()
                    elif self.type == "God":
                        other.god()
                    
                    
    def collideAstroid(self, other):
        if self.rect.right > other.rect.left and self.rect.left < other.rect.right:
            if self.rect.bottom > other.rect.top and self.rect.top < other.rect.bottom:
                if self.radius + other.radius > self.distanceToPoint(other.rect.center):
                    if self.rect.center[0] < other.rect.center[0]: #self left of other
                        if other.speedx < 0: #moving left
                            other.speedx = -other.speedx
                    if self.rect.center[0] > other.rect.center[0]: #self right of other
                        if other.speedx > 0: #moving right
                            other.speedx = -other.speedx
                    if self.rect.center[1] < other.rect.center[1]: #self above other
                        if other.speedy < 0: #moving up
                            other.speedy = -other.speedy
                    if self.rect.center[1] > other.rect.center[1]:#self below other
                        if other.speedy > 0: #moving down
                            other.speedy = -other.speedy
    
    def collideEnemy(self, other):
        if self.rect.right > other.rect.left and self.rect.left < other.rect.right:
            if self.rect.bottom > other.rect.top and self.rect.top < other.rect.bottom:
                if self.radius + other.radius > self.distanceToPoint(other.rect.center):
                    if self.rect.center[0] < other.rect.center[0]: #self left of other
                        if other.speedx < 0: #moving left
                            other.speedx = -other.speedx
                    if self.rect.center[0] > other.rect.center[0]: #self right of other
                        if other.speedx > 0: #moving right
                            other.speedx = -other.speedx
                    if self.rect.center[1] < other.rect.center[1]: #self above other
                        if other.speedy < 0: #moving up
                            other.speedy = -other.speedy
                    if self.rect.center[1] > other.rect.center[1]:#self below other
                        if other.speedy > 0: #moving down
                            other.speedy = -other.speedy
    
    def distanceToPoint(self, pt):
        x1 = self.rect.center[0]
        y1 = self.rect.center[1]
        x2 = pt[0]
        y2 = pt[1]
    
    def update(self):
        if self.lifetime > 0:
            self.lifetime -= 1
        else:
            self.living = False 